package cn.yuanqiao.activiti.listener;

import cn.yuanqiao.activiti.service.IActArchiveDataService;
import cn.yuanqiao.archive.archiveBorrow.domain.YqDaBorrow;
import cn.yuanqiao.archive.archiveBorrow.service.IYqDaBorrowService;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.exception.activiti.ArchiveProcessException;
import cn.yuanqiao.common.enums.BusinessBehavior;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WritsArchiveBorrowListener
 * @Description: 类的描述
 * @author: lihuahua
 * @date: 2023/6/5 19:24
 * @Blog: huahuacoco.top
 */

@Component("ArchiveBorrowAgreeListener")
public class ArchiveBorrowAgreeListener implements ExecutionListener {

    @Autowired
    private IActArchiveDataService actArchiveDataService;

    @Autowired
    private IYqDaBorrowService yqDaBorrowService;


    @Override
    public void notify(DelegateExecution execution) {
        String processInstanceId = execution.getProcessInstanceId();
        Object obj = execution.getVariable("archiveData");
        Object borrowDataObj = execution.getVariable("processVariables");

        if (obj == null){
            throw new ArchiveProcessException("档案数据不能为空");
        }else if (borrowDataObj == null){
            throw new ArchiveProcessException("借阅单数据不能为空");
        }
        Map<String,Object> borrowData = (Map<String, Object>) borrowDataObj;
        List<Map<String,Object>> archiveList  =(List<Map<String,Object>>)obj;
        List<String> archiveIdsNotAgree = new ArrayList<>();
        List<Map<String, Object>> archiveIdsAgree = new ArrayList<>();
        List<Map<String, Object>> archiveIdsNotAgreeMap = new ArrayList<>();
        for (Map<String, Object> archive: archiveList){
            if (archive.get("archiveAgreeStatus")!=null&&"0".equals(archive.get("archiveAgreeStatus"))){
                archiveIdsNotAgree.add(String.valueOf(archive.get("ID")));
                archiveIdsNotAgreeMap.add(archive);
            }else{
                archiveIdsAgree.add(archive);
            }
        }
        Long borrowId = (Long)borrowData.get("borrowId");
        Long borrowDays = Long.valueOf((Integer)borrowData.get("borrowDays"));
        String daType = String.valueOf(borrowData.get("daType"));
        String liyongfangshi = String.valueOf(borrowData.get("liyongfangshi"));
        if (daType.equals("F")&&liyongfangshi.equals("2")){
            actArchiveDataService.batchUpdateArchiveBorrowState(borrowData,SecurityUtils.getTenantCode(),"1",archiveIdsAgree);
            actArchiveDataService.batchUpdateArchiveBorrowState(borrowData,SecurityUtils.getTenantCode(),"0",archiveIdsNotAgreeMap);
        }
        YqDaBorrow yqDaBorrow = new YqDaBorrow();
        yqDaBorrow.setBorrowId(borrowId);
        if (borrowDays != null) {
            yqDaBorrow.setStartTime(DateUtils.getNowDate());
            yqDaBorrow.setEndTime(DateUtils.addDate(DateUtils.getNowDate(), borrowDays));
        }
        yqDaBorrowService.updateArchiveBorrowDate(yqDaBorrow);
        actArchiveDataService.batchUpdateBorrowState(borrowData,"0",archiveIdsNotAgree);
        actArchiveDataService.batchUpdateBorrowDownload(borrowData);
        System.out.println("同意借阅，修改数据");

        //添加过程信息
        actArchiveDataService.addBehavior(archiveIdsAgree, SecurityUtils.getTenantCode(),"ID","DA_TYPE",BusinessBehavior.LIYONG);
    }

}

